package com.lineage.server.timecontroller.npc;

import com.lineage.data.npc.quest2.Npc_DragonB1;
import com.lineage.data.npc.quest2.Npc_DragonB2;
import com.lineage.data.npc.quest2.Npc_DragonB3;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.types.Point;
import com.lineage.server.world.WorldNpc;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NpcDeleteTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(NpcDeleteTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 2000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 2000L, 2000L);
  }
  
  public void run() {
    try {
      Collection<L1NpcInstance> allNpc = WorldNpc.get().all();
      if (allNpc.isEmpty())
        return; 
      Iterator<L1NpcInstance> iter = allNpc.iterator();
      while (iter.hasNext()) {
        L1NpcInstance npc = iter.next();
        if (!npc.is_spawnTime())
          continue; 
        int time = npc.get_spawnTime();
        time -= 2;
        if (time > 0) {
          npc.set_spawnTime(time);
        } else {
          remove(npc);
        } 
        Thread.sleep(50L);
      } 
    } catch (Exception e) {
      _log.error("NPC存在时间时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      NpcDeleteTimer npcDeleteTimer = new NpcDeleteTimer();
      npcDeleteTimer.start();
    } 
  }
  
  private static void remove(L1NpcInstance tgnpc) {
    try {
      boolean isRemove = false;
      if (tgnpc instanceof com.lineage.server.model.Instance.L1MonsterInstance) {
        if (tgnpc.getNpcId() == 80034)
          tgnpc.outParty(tgnpc); 
        isRemove = true;
      } 
      if (tgnpc instanceof com.lineage.server.model.Instance.L1IllusoryInstance)
        isRemove = true; 
      if (isRemove) {
        tgnpc.setCurrentHpDirect(0);
        tgnpc.setDead(true);
        tgnpc.getMap().setPassable((Point)tgnpc.getLocation(), true);
        tgnpc.setExp(0L);
        tgnpc.setKarma(0);
        tgnpc.allTargetClear();
      } 
      if (tgnpc.getNpcId() == 70932) {
        tgnpc.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(tgnpc.getId(), 8));
        if (Npc_DragonB1._timer.containsKey(Integer.valueOf(tgnpc.get_quest_id())))
          Npc_DragonB1._timer.remove(Integer.valueOf(tgnpc.get_quest_id())); 
      } else if (tgnpc.getNpcId() == 70937) {
        tgnpc.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(tgnpc.getId(), 8));
        if (Npc_DragonB2._timer.containsKey(Integer.valueOf(tgnpc.get_quest_id())))
          Npc_DragonB2._timer.remove(Integer.valueOf(tgnpc.get_quest_id())); 
      } else if (tgnpc.getNpcId() == 70934) {
        tgnpc.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(tgnpc.getId(), 8));
        if (Npc_DragonB3._timer.containsKey(Integer.valueOf(tgnpc.get_quest_id())))
          Npc_DragonB3._timer.remove(Integer.valueOf(tgnpc.get_quest_id())); 
      } else if (tgnpc.getNpcId() == 70933) {
        tgnpc.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(tgnpc.getId(), 8));
      } 
      tgnpc.deleteMe();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
